package org.social.manager;

import java.util.ArrayList;
import java.util.List;

import org.bean.FunctionSwitchDTO;
import org.constant.ActionEnum;
import org.constant.ServerTypeEnum;
import org.remote.game.RemoteGameServer;
import org.remote.login.RemoteLoginServer;
import org.social.db.bean.FunctionSwitch;
import org.social.db.cache.FunctionSwitchCache;
import org.utils.RemoteUtil;

import com.google.common.collect.Lists;
import com.xiaoleilu.hutool.bean.BeanUtil;

/**
 * @author : DengYing
 * @CreateDate : 2018年2月8日 下午3:46:03
 * @Description ：功能开关管理器
 */
public class FunctionSwitchManager {
	
	public static void notifyRemote(ServerTypeEnum type,String serverId) {
		List<FunctionSwitch> list = FunctionSwitchCache.me().loadAll();
		List<FunctionSwitchDTO> notifyList = new ArrayList<>(list.size());
		FunctionSwitchDTO dto = null;
		for (FunctionSwitch functionSwitch : list) {
			dto = buildFunctionSwitchDTO(functionSwitch,ActionEnum.DEFAULT);
			notifyList.add(dto);
		}
		
//		if (!notifyList.isEmpty()) {
			switch (type) {
			case LOGIN_SERVER:
				RemoteUtil.loginAsyn().noticeSwitch(notifyList);				
				break;
			case GAME_SERVER:
				RemoteUtil.gameAsyn(serverId).noticeSwitch(notifyList);
				break;
			default:
				break;
			}
//		}
	}
	
	
	/** 添加屏蔽 
	 * @return */
    public static void changeFunctionSwitch(int funType,boolean isOpen, String remarks) {
        List<FunctionSwitchDTO> noticeList = Lists.newArrayList();
    	FunctionSwitch functionSwitch = FunctionSwitchCache.me().getByType(funType);
    	
    	ActionEnum action = null;
    	if (functionSwitch!=null) {
    		functionSwitch.setRemarks(remarks);
    		functionSwitch.setOpen(isOpen);
    		action = ActionEnum.UPDATE;
		}else {
			functionSwitch = new FunctionSwitch(0, funType, isOpen, remarks);
			functionSwitch.insert();
			action = ActionEnum.ADD;
		}
    	
    	noticeList.add(buildFunctionSwitchDTO(functionSwitch, action));
        // 通知
    	notifyAllServerFunctionSwitch(noticeList);
    }
    
    /** 删除开关 */
    public static void remove(int...ids) {
    	List<FunctionSwitchDTO> noticeList = new ArrayList<>(ids.length);
    	for (int id : ids) {
    		FunctionSwitch functionSwitch = FunctionSwitchCache.me().getById(id);
    		if (functionSwitch == null) {
				continue;
			}
    		functionSwitch.delete();
    		noticeList.add(buildFunctionSwitchDTO(functionSwitch, ActionEnum.REMOVE));
		}
    	// 通知
    	notifyAllServerFunctionSwitch(noticeList);
    }

	private static FunctionSwitchDTO buildFunctionSwitchDTO(FunctionSwitch functionSwitch, ActionEnum action) {
		FunctionSwitchDTO dto = new FunctionSwitchDTO();
		BeanUtil.copyProperties(functionSwitch, dto);
		dto.setAction(action);
		return dto;
	}
	
	public static void notifyAllServerFunctionSwitch(List<FunctionSwitchDTO> list) {
        RemoteGameServer remoteGameServer = RemoteUtil.gameTopic();
        remoteGameServer.noticeSwitch(list);
        
        RemoteLoginServer remoteLoginServer = RemoteUtil.loginAsyn();
        remoteLoginServer.noticeSwitch(list);
    }
}
